package howl.Servlets;

import howl.model.PMF;
import howl.types.*;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class HowlSigninServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		System.out.println(req);
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm
				.newQuery("select from howl.types.HowlUser "
						+ "where users == Param "
						+ "parameters User Param "
						+ "import howl.types.HowlUser; import com.google.appengine.api.users.User;");

		List<HowlUser> results = (List<HowlUser>) query.execute(user);
		System.out.println(results.size());
		if (user == null) {
			resp.sendRedirect(userService.createLoginURL("/Auth/index.jsp"));

		} else {
			if (results.size() > 0) {
				
				resp.sendRedirect("/Auth/index.jsp");
			} else {
				resp.sendRedirect("/Signup.jsp");
			}
		}
	}
}
